package hxg.thread;

import java.util.concurrent.*;

/**
 * @author HeXiangGuang
 * @create 2021-06-23 20:36
 * callable和Runnable接口的区别
 * (1)Callable接口中call方法有返回值,Runnable接口中run方法没有返回值
 * (2)callable接口中call方法有声明异常，Runnable接口中run方法没有异常
 */
public class Demo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        boolean flag = true;
        //1创建线程池
        ExecutorService es = Executors.newFixedThreadPool(1);
        //2.提交任务Future:表示要执行的任务结果
        Future<Integer> future = es.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println(Thread.currentThread().getName()+"开始计算！");
                int sum = 0;
                for (int i = 1; i <=100 ; i++) {
                    sum+=i;
                }
                return sum;
            }
        });
        //3获取任务结果
        System.out.println(future.get());
        //4关闭线程池
        es.shutdown();

    }
}
